//
//  ExaminaColumnFirstCell.m
//  Coach_HaiGeEr
//
//  Created by liuchen on 2018/10/19.
//  Copyright © 2018年 海格尔. All rights reserved.
//

#import "ExaminaColumnFirstCell.h"
@interface ExaminaColumnFirstCell()
@property (nonatomic,weak) UILabel *nameAndPhoneLabel;
@property (nonatomic,weak) UILabel *carAndExaminaLabel;
@property (nonatomic,weak) UILabel *resonLabel;
@property (nonatomic,weak) UIView *stateView;
@end

@implementation ExaminaColumnFirstCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return self;
}

#pragma mark ==========创建视图==========
- (void)createSubView{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    UILabel *nameAndPhoneLabel = [[UILabel alloc] init];
    nameAndPhoneLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    nameAndPhoneLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    nameAndPhoneLabel.textAlignment = NSTextAlignmentLeft;
    _nameAndPhoneLabel = nameAndPhoneLabel;
    [self.contentView addSubview:self.nameAndPhoneLabel];
    
    UILabel *carAndExaminaLabel = [[UILabel alloc] init];
    carAndExaminaLabel.textColor = [UIColor colorWithHexString:@"#282828"];
    carAndExaminaLabel.font = [UIFont systemFontOfSize:LoginFont-3];
    carAndExaminaLabel.textAlignment = NSTextAlignmentLeft;
    _carAndExaminaLabel = carAndExaminaLabel;
    [self.contentView addSubview:self.carAndExaminaLabel];
    
    WS(ws);
    [self.nameAndPhoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(ws.contentView).with.offset(25*SCALEUNIT);
        make.top.mas_equalTo(ws.contentView).with.offset(22*SCALEUNIT);
        make.right.mas_equalTo(ws.contentView).with.offset(-175*SCALEUNIT);
        make.height.mas_equalTo(50*SCALEUNIT);
    }];
    [self.carAndExaminaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.mas_equalTo(ws.nameAndPhoneLabel);
        make.top.mas_equalTo(ws.nameAndPhoneLabel.mas_bottom);
    }];
}

#pragma mark ==========填充数据==========
- (void)setModel:(TeachManagementModel *)model{//今日考试+教练待审核
    _model = model;
    self.nameAndPhoneLabel.attributedText = [self returnAttritubeWithTitleOne:@"姓名" WithTitleTwo:@"电话" WithContentOne:_model.studentName WithContentTwo:_model.phoneNo WithJudge:YES];
    self.carAndExaminaLabel.attributedText = [self returnAttritubeWithTitleOne:@"报考车型" WithTitleTwo:@"考试类型" WithContentOne:_model.applicationCardType WithContentTwo:_model.examType==1?@"初次":@"补考" WithJudge:YES];
    [self returnStatusViewWithJudge:3];
}
- (void)setSchoolModel:(TeachManagementModel *)schoolModel{//驾校待审核
    _schoolModel = schoolModel;
    self.nameAndPhoneLabel.attributedText = [self returnAttritubeWithTitleOne:@"姓名" WithTitleTwo:@"电话" WithContentOne:_schoolModel.studentName WithContentTwo:_schoolModel.phoneNo WithJudge:YES];
    self.carAndExaminaLabel.attributedText = [self returnAttritubeWithTitleOne:@"报考车型" WithTitleTwo:@"考试类型" WithContentOne:_schoolModel.applicationCardType WithContentTwo:_schoolModel.examType==1?@"初次":@"补考" WithJudge:YES];
}
- (void)setAlreadyExamineModel:(TeachManagementModel *)alreadyExamineModel{//已审核
    _alreadyExamineModel = alreadyExamineModel;
    self.nameAndPhoneLabel.attributedText = [self returnAttritubeWithTitleOne:@"姓名" WithTitleTwo:@"电话" WithContentOne:_alreadyExamineModel.studentName WithContentTwo:_alreadyExamineModel.phoneNo WithJudge:YES];
    self.carAndExaminaLabel.attributedText = [self returnAttritubeWithTitleOne:@"报考车型" WithTitleTwo:@"" WithContentOne:_alreadyExamineModel.applicationCardType WithContentTwo:@"" WithJudge:NO];
    if (_alreadyExamineModel.coachOrSchool==1?!_alreadyExamineModel.caochCheckResult:!_alreadyExamineModel.schoolCheckResult) {
        [self returnResonViewWithContent:[NSString stringWithFormat:@"拒绝原因:%@",_alreadyExamineModel.coachOrSchool==1?_alreadyExamineModel.coachUnpassReason:_alreadyExamineModel.schoolUnpassReason]];
    }
    [self returnStatusViewWithJudge:_alreadyExamineModel.coachOrSchool==1?_alreadyExamineModel.caochCheckResult:_alreadyExamineModel.schoolCheckResult];
}
- (void)setWaitExaminationModel:(TeachManagementModel *)waitExaminationModel{//待考试
    _waitExaminationModel = waitExaminationModel;
    self.nameAndPhoneLabel.attributedText = [self returnAttritubeWithTitleOne:@"姓名" WithTitleTwo:@"电话" WithContentOne:_waitExaminationModel.studentName WithContentTwo:_waitExaminationModel.phoneNo WithJudge:YES];
    self.carAndExaminaLabel.attributedText = [self returnAttritubeWithTitleOne:@"报考车型" WithTitleTwo:@"考试时间" WithContentOne:_waitExaminationModel.applicationCardType WithContentTwo:_waitExaminationModel.examDateStr WithJudge:YES];
}

#pragma mark ==========添加拒绝原因视图==========
- (void)returnResonViewWithContent:(NSString *)content{
    if (self.resonLabel!=nil) return;
    UILabel *resonLabel = [[UILabel alloc] init];
    resonLabel.textAlignment = NSTextAlignmentCenter;
    resonLabel.textColor = [UIColor redColor];
    resonLabel.font = [UIFont systemFontOfSize:LoginFont-4];
    resonLabel.textAlignment = NSTextAlignmentLeft;
    resonLabel.text = content;
    _resonLabel = resonLabel;
    [self.contentView addSubview:self.resonLabel];
    WS(ws);
    [resonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(ws.carAndExaminaLabel);
        make.top.mas_equalTo(ws.carAndExaminaLabel.mas_bottom).with.offset(20*SCALEUNIT);
        make.height.mas_equalTo(40*SCALEUNIT);
    }];
}

#pragma mark ==========操作视图==========
- (void)returnStatusViewWithJudge:(NSInteger)judge{//150*120  0未通过 1已通过 3正常点击
    if (self.stateView!=nil) return;
    UIView *stateView = [[UIView alloc] init];
    _stateView = stateView;
    [self.contentView addSubview:self.stateView];
    WS(ws);
    [stateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25*SCALEUNIT);
        make.centerY.mas_equalTo(ws.contentView);
        make.size.mas_equalTo(CGSizeMake(150*SCALEUNIT, 120*SCALEUNIT));
    }];
    if (judge==3) {
        for (int i=0; i<2; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:self.model.todayExamina==1?@"TeachExaminaPass%@Image":@"TeachExaminaAgree%@Image",i==0?@"One":@"Two"]] forState:UIControlStateNormal];
            btn.showsTouchWhenHighlighted = YES;
            btn.tag = 10000+i;
            [btn addTarget:self action:@selector(actionChange:) forControlEvents:UIControlEventTouchUpInside];
            [stateView addSubview:btn];
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.mas_equalTo(stateView);
                make.top.mas_equalTo(stateView).with.offset(70*SCALEUNIT*i);
                make.size.mas_equalTo(CGSizeMake(150*SCALEUNIT, 50*SCALEUNIT));
            }];
        }
    } else if (judge == 1) {
        UILabel *stateLabel = [[UILabel alloc] init];
        stateLabel.font = [UIFont systemFontOfSize:LoginFont-3];
        stateLabel.text = @"已通过";
        stateLabel.textAlignment = NSTextAlignmentRight;
        stateLabel.textColor = [UIColor colorWithHexString:@"#21d4a4"];
        [stateView addSubview:stateLabel];
        [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(stateView);
            make.top.mas_equalTo(stateView).with.offset(10*SCALEUNIT);
            make.size.mas_equalTo(CGSizeMake(150*SCALEUNIT, 50*SCALEUNIT));
        }];
    } else {
        UILabel *stateLabel = [[UILabel alloc] init];
        stateLabel.font = [UIFont systemFontOfSize:LoginFont-3];
        stateLabel.text = @"未通过";
        stateLabel.textAlignment = NSTextAlignmentRight;
        stateLabel.textColor = [UIColor redColor];
        [stateView addSubview:stateLabel];
        [stateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(stateView);
            make.top.mas_equalTo(stateView).with.offset(10*SCALEUNIT);
            make.size.mas_equalTo(CGSizeMake(150*SCALEUNIT, 50*SCALEUNIT));
        }];
    }
}

#pragma mark ==========通过+未通过==========
- (void)actionChange:(UIButton *)btn{
    if ([self.delegate respondsToSelector:@selector(examinaColumnFirstCellDelegateWithState:WithID:)]) {
        [self.delegate examinaColumnFirstCellDelegateWithState:btn.tag-10000==0?1:0 WithID:self.model.ID];
    }
}


- (NSMutableAttributedString *)returnAttritubeWithTitleOne:(NSString *)titleOne WithTitleTwo:(NSString *)titleTwo WithContentOne:(NSString *)contentOne WithContentTwo:(NSString *)contentTwo WithJudge:(BOOL)judge{
    NSString *content = judge?[NSString stringWithFormat:@"%@:%@      %@:%@",titleOne,contentOne,titleTwo,contentTwo]:[NSString stringWithFormat:@"%@:%@",titleOne,contentOne];
    //初始化
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:content];
    //设置范围
    NSRange rangeOne = [content rangeOfString:[NSString stringWithFormat:@"%@:",titleOne]];
    //为某一范围内文字设置多个属性
    NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:                           [UIColor colorWithHexString:@"#999999"], NSForegroundColorAttributeName,                           [UIFont systemFontOfSize:LoginFont-3], NSFontAttributeName, nil];
    [str setAttributes:attrs range:rangeOne];
    if (judge) {
        NSRange rangeTwo;
        rangeTwo = [content rangeOfString:[NSString stringWithFormat:@"   %@:",titleTwo]];
        [str setAttributes:attrs range:rangeTwo];
    }
    return str;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
